import util from '@/static/utils/util'
import {
	getTokenByCode,
	register
} from "@/api/user.js"
import {
	getInfo as paymentGetInfo
} from "@/api/payment.js"





export default{
	state:{
		token:'',
		uuid:'',
		mchId:'10205',
		userId:'',
		xuid:'',
		userInfo:{
			avatar:'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ5mponqTImpsvliciaNkSxNYUaTWrh43K3EwZjp1cRQu0hjxwWqd5bGcxeAiazaLb3hOSZwmzGxy33g/132'
		},
		nickname:'',
		encryptData:'',
		iv:'',
		code:'',
		paymentGetInfo:{}
	},
	getters:{},
	mutations:{
		callRegister(state,e){
			var _this = this
			// #ifdef MP-WEIXIN
			let params={
				mchId: state.mchId,
				uuid: state.uuid,
				xuid:state.xuid,
				nickname:state.nickname,
				encryptData: e.detail.encryptedData,
				iv: e.detail.iv,
				code: state.code,
			}
			// #endif
			// #ifdef MP-ALIPAY
			let params={
				mchId: state.mchId,
				uuid: state.uuid,
				encryptData: e.detail.encryptedData,
				iv: e.detail.sign,
			}
			// #endif
			register(params).then(
				res => {
					console.log(res,"8888888888888888888888888")
					if (res.type == 'success') {
						state.token = res.data.token
						state.userInfo = res.data.userInfo
						state.userId = res.data.userId
						uni.setStorage({
							key: 'storage_key',
							data: state.token,
							success() { //把token 存入本地
								const token = uni.getStorageSync('storage_key');
								// 判断是否登录
								if (token) {
									uni.navigateBack({
										delta: 1,
									})
								}
							}
						});
					} else {
						uni.showToast({
							icon: "none",
							title: res.content
						})
					}
				}
			).catch(error => {
			})
		},
		Initialization(state){
			state.uuid = util.getUUID()
			// #ifdef MP-WEIXIN
			wx.login({
				success: res => {
					getTokenByCode({
						mchId:state.mchId,
						auth_code: res.code,
						uuid: state.uuid,
						pluginId: 'weixinMiniAuthPlugin'
					}).then(
						res => {
							state.token = res.data.token
							state.userInfo = res.data.userInfo
							state.userId = res.data.userId
							console.log(state.userId,"userIduserIduserIduserIduserId")
							uni.setStorage({
								key: 'storage_key',
								data: state.token,
								success() { //把token 存入本地
									const token = uni.getStorageSync('storage_key');
									// 判断是否登录
									if (token) {
										uni.navigateBack({
											delta: 1,
										})
									}
								}
							});
							console.log(res,'获取token')
							console.log('3')
							paymentGetInfo().then(res=>{
								state.paymentGetInfo = res.data
							})
						}
					).catch(error => {
					})
				},
				fail: err => {
				}
			})
			// #endif
			// #ifdef MP-ALIPAY
			let self = this;
			uni.getProvider({
				service: 'oauth',
				success: function (res) {
					self.provider = res.provider[0];
					uni.login({
						provider: self.provider,
						success: function (loginRes) {
							console.log('uniapp统一登录凭证',loginRes)
							// 获取用户基础信息 无需授权 不含code
							uni.getUserInfo({
								provider: self.provider,
								success(infoRes) {
									// 测试只有在微信端能拿到 支付宝端拿不到
									console.info('uniapp用户基础信息',infoRes)
									// #ifdef MP-ALIPAY
									my.getAuthCode({
										scopes: 'auth_user',
										success: (auth) => {
											console.log('支付宝授权凭证',auth)
											console.log(auth,"321321321321")
											getTokenByCode({
												mchId:state.mchId,
												auth_code: auth.authCode,
												uuid: state.uuid,
												pluginId: 'alipayMiniAuthPlugin'
											}).then(
												res => {
													state.token = res.data.token
													state.userInfo = res.data.userInfo
													uni.setStorage({
														key: 'storage_key',
														data: state.token,
														success() { //把token 存入本地
															const token = uni.getStorageSync('storage_key');
															console.log(token,"321321321")
															// 判断是否登录
															if (token) {
																uni.navigateBack({
																	delta: 1,
																})
															}
														}
													});
													if(state.token != null){
														paymentGetInfo().then(res=>{
															state.paymentGetInfo = res.data
														})
													}
												}
											).catch(error => {
												console.log(error)
												console.log('41321321')
											})
										},
									});
									// #endif
								}
							});
						}
					});
				}
			});
			// #endif
		},
		changeInitialization(state,res){
			console.log(res,'授权获取token')
			state.token = res.data.token
			state.userInfo = res.data.userInfo
			uni.setStorage({
				key: 'storage_key',
				data: state.token,
				success() { //把token 存入本地
					const token = uni.getStorageSync('storage_key');
					console.log(token,"321321321")
					// 判断是否登录
					if (token) {
						uni.navigateBack({
							delta: 1,
						})
					}
				}
			});
			uni.setStorageSync('userInfo',JSON.stringify(state.userInfo));
		}
	},
	actions:{}
}
